<template>
  <section>
    <!--工具条：高级查询，上下架-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="searchForm">
        <el-form-item>
          <el-input v-model="searchForm.keyword" placeholder="领养订单名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="keywordQuery">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表展示-->
    <el-table :data="adoptOrders" highlight-current-row v-loading="listLoading" style="width: 100%;">
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="pet.name" label="名称" width="160" sortable>
      </el-table-column>
      <el-table-column prop="price" label="价格" width="160" sortable>
      </el-table-column>
      <!--:formatter="formatState"-->
      <el-table-column prop="state" label="状态" width="150"  sortable>
        <!--待支付0 待确认1 完成2 取消-1-->
        <template scope="scope">
          <span style="color: green" v-if="scope.row.state==0">待支付</span>
          <span style="color: red" v-else-if="scope.row.state==1">待确认</span>
          <span style="color: orangered" v-else-if="scope.row.state==2">完成</span>
          <span style="color: darkblue" v-else>取消</span>
        </template>
      </el-table-column>
      <!--关联查询-后台查询出来后，前台只需要通过关系属性.要显示的字段名就-->
      <el-table-column prop="user.username" label="用户" width="160" sortable>
      </el-table-column>
      <el-table-column prop="shop.name" label="店铺" width="160" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" type="success" @click="handleEdit(scope.$index, scope.row)">查 看</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 工具条 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="searchForm.pageSize" :total="total" :current-page="searchForm.currentPage" style="float:right;">
      </el-pagination>
    </el-col>
    <!--添加和修改的模态框-->
    <el-dialog title="查看"  :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="adoptOrderForm" label-width="80px"  ref="adoptOrderForm">
<!--  readonly 只读      -->
        <el-form-item label="摘要">
          <el-input v-model="adoptOrderForm.digest" readonly auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="adoptOrderForm.price" readonly auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="adoptOrderForm.state">
            <el-radio class="radio" :label="0">待支付</el-radio>
            <el-radio class="radio" :label="2">待确认</el-radio>
            <el-radio class="radio" :label="1">完成</el-radio>
            <el-radio class="radio" :label="-1">取消</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="订单编号">
          <el-input v-model="adoptOrderForm.orderSn" readonly auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="最后支付时间">
          <el-input v-model="adoptOrderForm.lastPayTime" readonly auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="宠物名称">
          <el-input v-model="adoptOrderForm.pet.name" readonly auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="领养用户">
          <el-input v-model="adoptOrderForm.user.username" readonly auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="领养店铺">
          <el-input v-model="adoptOrderForm.shop.name" readonly auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {
      //分页+高级查询的数据 - 要传递到后端的
      searchForm: {
        keyword: null,//关键字
        pageSize: 5,//每页显示的条数
        currentPage: 1 //当前页
      },
      //分页列表的数据
      adoptOrders: [], //分页查询列表数据
      listLoading: false, //分页查询 - 列表忙等框
      //分页栏需要的数据
      total: 0,

      //添加和修改模态框数据
      editFormVisible: false,//模态框是否显示 - 默认不显示
      editLoading: false, //忙等框 - 默认不显示
      editFormRules: { //模态框验证规则
        name: [
          { required: true, message: '请输入领养订单名称', trigger: 'blur' }
        ]
      },
      //添加和修改模态框数据
      adoptOrderForm: {
        digest: null,
        state: 0,
        price: 0,
        orderSn: null,
        lastPayTime: null,
        pet: {},
        user: {},
        shop: {}
      }
    }
  },
  methods: {
    //点击某一页的处理
    handleCurrentChange(val) {
      //将点击的某一页赋值给当前页
      this.searchForm.currentPage = val;
      this.getAdoptOrders();
    },

    //高级查询 + 分页查询
    getAdoptOrders() {
      //显示列表忙等框
      this.listLoading = true;
      //发送异步请求加载列表数据
      this.$http.post("/adoptOrder/admin",this.searchForm).then(res=>{
        this.adoptOrders=res.data.rows;
        this.total=res.data.total;
        //关闭列表忙等框
        this.listLoading = false;
      });
    },

    //高级查询
    keywordQuery(){
      //当前页为1
      this.searchForm.currentPage=1,
      this.getAdoptOrders()
    },

    //点击编辑按钮显示模态框
    handleEdit: function (index, row) {
      //进行数据回显：将row【本省就是一个json对象  克隆 给一个空对象{}】,防止引用传递导致的数据变化
      this.adoptOrderForm = Object.assign({}, row);
      this.editFormVisible = true;
    }
  },
  mounted() {
    //当页面加载完以后。执行的方法
    this.getAdoptOrders();
  }
}

</script>

<style scoped>

</style>